package cdu.xjj.controller;


import cdu.xjj.pojo.dto.UserAddDTO;
import cdu.xjj.pojo.dto.UserLoginDTO;
import cdu.xjj.pojo.dto.UserUpdateDTO;
import cdu.xjj.pojo.vo.LoginVO;
import cdu.xjj.result.Result;
import cdu.xjj.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/users")
@Slf4j
public class UserController {

    private final UserService userService;

    @PostMapping("/add")
    public Result signUp(@RequestBody UserAddDTO userAddDTO) {
        log.info("新增用户：{}", userAddDTO);
        userService.save(userAddDTO);
        return Result.success();
    }

    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("用户登录:{}", userLoginDTO);
        LoginVO loginVO = userService.login(userLoginDTO);
        return Result.success(loginVO);
    }


    @PutMapping
    public Result updateUserInfo(@RequestBody UserUpdateDTO userUpdateDTO) {
        log.info("更新用户信息：{}", userUpdateDTO);
        userService.updateUserInfo(userUpdateDTO);
        return Result.success();
    }


    @GetMapping("/test")
    public Result test() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return Result.success(authentication);
    }


}
